Sample DCGAN Implementation using Keras

To illustrate the implementation of a GAN using Python, we use the Deep Convolutional GAN example discussed in the Section 'Evolution of GAN Architectures' to synthesize images from the fashion MNIST dataset.

Adapted from https://github.com/eriklindernoren/Keras-GAN/blob/master/dcgan/dcgan.py

Imports & Settings

Helper

Build Discriminator

Both the discriminator and generator use a deep CNN architecture, wrapped in a function:

A call to this function and subsequent compilation shows that this network has over 393,000 parameters.

Build Generator

The generator network is slightly shallower but has more than twice as many parameters:

Create Combined Model

The combined model consists of the stacked generator and discriminator and trains the former to fool the latter:

Load the Data

Adversarial Training

Adversarial training iterates over the epochs, generates random image and noise input, and trains both the discriminator and the generator (as part of the combined model):

After 4,000 epochs, which only takes a few minutes, the synthetic images created from random noise clearly resemble the originals: